import { Message } from 'element-ui'

let messageInstance = null
export default {
	/**
	 * 消息提示
	 * @param msg
	 * @param type
	 */
	_msgBox(msg, type) {
		if(messageInstance) {
			// 如果已经存在一个消息提示实例，则先关闭它
			Message.closeAll()
		}
		let msgType = 'success'
		let typeList = ['success', 'warning', 'info', 'error']
		if(type && isNaN(Number(type)) && typeList.includes(type)) {
			msgType = type
		} else if(type && !isNaN(Number(type)) && Number(type) < typeList.length) {
			msgType = typeList[Number(type)]
		} else if((type && isNaN(Number(type)) && !typeList.includes(type)) || (type && !isNaN(Number(type)) && Number(type) >= typeList.length)) {
			throw new Error('type参数错误')
		}
		// 显示新的消息提示，并保存该实例
		messageInstance = Message({
			message: msg,
			type: msgType,
			showClose: true,
			duration: 1500,
			onClose: () => {
				messageInstance = null // 当消息提示关闭时，将实例置为null
			}
		})
	}
}